﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace mal.WP7.Framework.Controls
{
    public partial class BarChartItem : UserControl
    {
        #region Fields

        #endregion

        #region Construction / Destruction
        public BarChartItem()
        {
            InitializeComponent();

            this.MouseEnter += new System.Windows.Input.MouseEventHandler(BarChartItem_MouseEnter);
            this.MouseLeave += new System.Windows.Input.MouseEventHandler(BarChartItem_MouseLeave);
        }


        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public Brush Fill
        {
            get
            {
                return (Brush)GetValue(FillProperty);
            }
            set
            {

                SetValue(FillProperty, value);
            }
        }

        public static readonly DependencyProperty FillProperty
    = DependencyProperty.Register(
                              "Fill", typeof(Brush),
                              typeof(BarChartItem),
                              new PropertyMetadata(null, FillChanged)
                            );


        public Brush HighlightFill
        {
            get
            {
                return (Brush)GetValue(HighlightFillProperty);
            }
            set
            {

                SetValue(HighlightFillProperty, value);
            }
        }


        public static readonly DependencyProperty HighlightFillProperty
= DependencyProperty.Register(
         "Fill", typeof(Brush),
         typeof(BarChartItem),
         new PropertyMetadata(null, HighlightFillChanged)
       );
        #endregion

        #region Methods
        internal void Start()
        {
            Storyboard storyboard = (Storyboard)LayoutRoot.Resources["expandStoryboard"];

            DoubleAnimationUsingKeyFrames keyframes = (DoubleAnimationUsingKeyFrames)storyboard.Children[0];
            Random rnd = new Random();

            storyboard.BeginTime = TimeSpan.FromMilliseconds(rnd.Next(0, 200));

            keyframes.KeyFrames[1].Value = this.Height * 1.05;
            keyframes.KeyFrames[2].Value = this.Height * 0.95;
            keyframes.KeyFrames[3].Value = this.Height * 1;

            storyboard.Begin();

        }

        private static void FillChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((BarChartItem)d).SetFill((Brush)e.NewValue);
        }

        private static void HighlightFillChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((BarChartItem)d).HighlightFill = (Brush)e.NewValue;
        }


        private void SetFill(Brush value)
        {
            column.Fill = value;
            column.Height = 0;
        }
        #endregion

        #region Events
        private void BarChartItem_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {

        }

        private void BarChartItem_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {

        }
        #endregion
    }
}
